package arsmodeller.model;

import java.awt.geom.Line2D;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.swt.graphics.Color;

public class Connection implements IAdaptable {

	public static final String PROPERTY_LAYOUT = "ConnectionLayout";
	public static final String PROPERTY_SOURCE = "ConnectionAddChild";
	public static final String PROPERTY_TARGET = "ConnectionRemoveChild";
	public static final String PROPERTY_NAME = "ConnectionName";
	public static final String PROPERTY_DESCRIPTION = "ConnectionDescription";
	public static final String PROPERTY_COLOR = "ConnectionColor";
	public static final String PROPERTY_TYPE = "ConnectionType";
	public static final int CONNECTION_DESIGN = 1;
	public static final int CONNECTION_RESOURCES = 2;
	public static final int CONNECTION_WORKPACKAGES = 3;
	private Node source;
	private Node oldSource;
	private Node target;
	private Node oldTarget;
	private String name;
	private String oldName;
	private String description;
	private String oldDescription;
	private Color color;
	private Color oldcolor;
	private PropertyChangeSupport listeners;
	private Line2D line;
	private int oldConnectionType;
	private int connectionType;

	public Connection() {
		this.source = new Node();
		this.target = new Node();
		this.name = "default";
		this.description = "no description";
		this.listeners = new PropertyChangeSupport(this);
	}

	public Connection(Node source, Node target) {
		if (source != null && target != null) {
			this.source = source;
			this.target = target;
			this.listeners = new PropertyChangeSupport(this);
		}
	}

	public Connection(String name, String description, Color colour) {
		if (name != null && description != null && colour != null) {
			this.name = name;
			this.description = description;
			this.color = colour;
			this.listeners = new PropertyChangeSupport(this);
		}
	}

	public void connect() {
		source.addConnections(this);
		target.addConnections(this);
	}

	public void disconnect() {
		source.removeConnection(this);
		target.removeConnection(this);
	}

	@Override
	public Object getAdapter(Class adapter) {
		// TODO Auto-generated method stub
		return null;
	}

	public Node getSource() {
		return source;
	}

	public void setSource(Node source) {
		this.oldSource = this.source;
		this.source = source;
		getListeners().firePropertyChange(PROPERTY_SOURCE, oldSource, source);

	}

	public Node getTarget() {
		return target;
	}

	public void setTarget(Node target) {
		this.oldTarget = this.target;
		this.target = target;
		getListeners().firePropertyChange(PROPERTY_TARGET, oldTarget, target);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.oldName = this.name;
		this.name = name;
		getListeners().firePropertyChange(PROPERTY_NAME, oldName, name);

	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.oldDescription = this.description;
		this.description = description;
		getListeners().firePropertyChange(PROPERTY_DESCRIPTION, oldDescription,
				description);
	}

	public void reconnect(Node sourceNode, Node targetNode) {
		if (sourceNode == null || targetNode == null
				|| sourceNode == targetNode) {
			throw new IllegalArgumentException();
		}
		disconnect();
		this.source = sourceNode;
		this.target = targetNode;
		connect();
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.oldcolor = this.color;
		this.color = color;
		getListeners().firePropertyChange(PROPERTY_COLOR, oldcolor, color);
	}

	private PropertyChangeSupport getListeners() {
		return this.listeners;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		listeners.addPropertyChangeListener(listener);

	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		listeners.removePropertyChangeListener(listener);

	}

	public int getConnectionType() {
		return connectionType;
	}

	public void setConnectionType(int connectionType) {
		this.oldConnectionType = this.connectionType;
		this.connectionType = connectionType;
		getListeners().firePropertyChange(PROPERTY_TYPE, oldConnectionType,
				connectionType);
	}
}
